import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;

import org.apache.log4j.PropertyConfigurator;
import org.fbk.it.hlt.jlsi.data.TextCorpusReader;

public class CorpusReaderTest {
	public static void main(String[] args) throws IOException {
		File output_dir = new File(System.getProperty("user.home")
				+ "/pubmed_result/BCL2_Angiogenesis/");
		String logConfig = "log-config.txt";
		new File("tmp/").mkdirs();
		String output = "tmp/X";
		File stop = new File("stop-words.txt");
		long begin = System.currentTimeMillis();

		PropertyConfigurator.configure(logConfig);

		TextCorpusReader tcr = new TextCorpusReader(output_dir, output, stop,
				10);
		int expectedAutovalues = tcr.columnCount / 10;
		if (expectedAutovalues > 100)
			expectedAutovalues = 100;
		if (expectedAutovalues < 2)
			expectedAutovalues = 2;
		long end = System.currentTimeMillis();
		System.out.println("corpus readDocumentList in " + (end - begin)
				+ " ms\nStarting svd");
		Runtime envir = Runtime.getRuntime();
		Process p = null;

		p = envir.exec("SVDLIBC/svd -v 0 -d " + expectedAutovalues + " -o "
				+ output + " -r sb " + output + "-matrix");
		// System.out.println("SVDLIBC/svd -v 0 -d "+expectedAutovalue+" -o tmp/X -r sb tmp/X-matrix");
		BufferedInputStream br = new BufferedInputStream(p.getInputStream());
		byte[] autovalori = new byte[10];
		int size = br.read(autovalori);
		StringBuilder result = new StringBuilder();
		for (int i = 0; i < size - 1; i++) {
			// result.append(separator);
			result.append((char) autovalori[i]);
		}

		int autovalues = Integer.parseInt(result.toString());
		p.destroy();
		// String result = new String(autovalori);
		if (expectedAutovalues > autovalues) {
			System.out
					.println("Numero di autovalori minore del previsto, rilancio programma");
			p = envir.exec("SVDLIBC/svd -v 0 -d " + autovalues + " -o "
					+ output + " -r sb " + output + "-matrix");
		} else
			System.out
					.println("Numero di autovalori desiderato sufficiente, eseguito");

	}
}
